﻿using System;
using System;
using System.Collections.Generic;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Text;
using netDxf;
using netDxf.Blocks;
using netDxf.Collections;
using netDxf.Entities;
using netDxf.Header;
using netDxf.Objects;
using netDxf.Tables;
using netDxf.Units;
using Attribute = netDxf.Entities.Attribute;
using Image = netDxf.Entities.Image;
using Point = netDxf.Entities.Point;
using Trace = netDxf.Entities.Trace;
using System.Threading;

namespace TestDxfDocument
{

    class Lensmillsolid
    {
        public static void _Lensmillsolid(Dictionary<string, Object> data)//细柄丝锥
        {
            DxfDocument dxf = new DxfDocument();
            //图层
            Layer contour = new Layer("contour") { Color = AciColor.Cyan };    //轮廓层
            Layer dimensioning = new Layer("dimensioning");    //尺寸层
            Layer refin = new Layer("refin") { Color = AciColor.Red };    //基准线/内轮廓
            refin.Linetype = Linetype.Dashed;
            Layer centre = new Layer("centre") { Color = AciColor.Yellow };    //中心线
            Layer textlayer = new Layer("textlayer") { Color = AciColor.Blue };    //文本层
            Layer hatching = new Layer("hatching") { Color = AciColor.Blue };    //填充层
            centre.Linetype = Linetype.DashDot;
            Layer additionalline = new Layer("additionalline") { Color = AciColor.Cyan };    //附加线
            Layer cutting = new Layer("cutting") { Color = AciColor.Red };    //切削层
            Layer nocutting = new Layer("nocutting");    //非切削层
            Layer skcontour = new Layer("skcontour") { Color = AciColor.Cyan };    //sk轮廓层
            Layer skdimensioning = new Layer("skdimensioning");    //sk尺寸线层
            Layer skrefin = new Layer("skrefin") { Color = AciColor.Red };    //sk基准线/内轮廓
            Layer sktext = new Layer("sktext") { Color = AciColor.Blue };  //sk文本
            Layer skhatching = new Layer("skhatching") { Color = AciColor.Blue };    //sk填充
            Layer skadditionlline = new Layer("skadditionlline") { Color = AciColor.Cyan };    //sk附加线
            Layer skviewcontour = new Layer("skviewcontour") { Color = AciColor.Cyan };    //skview轮廓
            Layer skviewdimensioning = new Layer("skviewdimensioning");    //skview尺寸线层

            //参数
            double pi = Math.PI;
            string path = Convert.ToString(data["path"]);

            double scale = 2; //放大系数

            double oal = 37.24;
            double lpr = 13.33;
            double lh = 8.35;
            double dconms = 6.21;
            double dc = 6.21;

            //坐标
            Vector2 v1 = new Vector2(0, 0);
            Vector2 v2 = new Vector2(v1.X + oal + 4, v1.Y);
            Vector2 v3 = new Vector2(v1.X + 2, v1.Y);
            Vector2 v4 = new Vector2(v3.X, v3.Y + dconms / 2 - 0.5);
            Vector2 v5 = new Vector2(v3.X + 0.5, v3.Y + dconms / 2);
            Vector2 v8 = new Vector2(v3.X + oal, v3.Y);
            Vector2 v7 = new Vector2(v8.X - lh / 27.833, v8.Y + dc / 2.266);
            Vector2 v6 = new Vector2(v8.X - lh / 10.844, v8.Y + dc / 2);
            Vector2 v9 = new Vector2(v7.X, v7.Y * -1);
            Vector2 v10 = new Vector2(v6.X, v6.Y * -1);
            Vector2 v11 = new Vector2(v5.X, v5.Y * -1);
            Vector2 v12 = new Vector2(v4.X, v4.Y * -1);
            Vector2 v13 = new Vector2(v8.X - lh / 23.857, v8.Y - dc / 3.741);
            Vector2 v14 = new Vector2(v8.X - lh / 18.152, v8.Y - dc / 3);
            Vector2 v15 = new Vector2(v10.X, v10.Y + dc / 8.171);
            Vector2 v16 = new Vector2(v10.X - lh / 1.807, v10.Y);
            Vector2 v17 = new Vector2(v15.X, v15.Y + dc / 12.176);
            Vector2 v18 = new Vector2(v17.X - lh / 30.926, v17.Y + dc / 11.942);
            Vector2 v19 = new Vector2(v17.X - lh / 23.194, v17.Y + dc / 6.606);
            Vector2 v20 = new Vector2(v8.X - lh / 10.987, v8.Y - dc / 17.743);
            Vector2 v21 = new Vector2(v20.X + lh / 55.667, v20.Y + dc / 12.42);
            Vector2 v22 = new Vector2(v21.X + lh / 36.304, v21.Y + dc / 8.065);
            Vector2 v23 = new Vector2(v8.X - lh / 27.833, v8.Y + dc / 5.175);
            Vector2 v24 = new Vector2(v21.X - lh / 64.231, v21.Y + dc / 16.784);
            Vector2 v25 = new Vector2(v21.X - lh / 36.304, v21.Y + dc / 7.056);
            Vector2 v26 = new Vector2(v21.X - lh / 30.926, v21.Y + dc / 5.008);
            Vector2 v27 = new Vector2(v8.X - lh, v10.Y);
            Vector2 v28 = new Vector2(v27.X + lh / 2.554, v27.Y + dc / 4.814);
            Vector2 v29 = new Vector2(v28.X, v28.Y + dc / 13.8);
            Vector2 v30 = new Vector2(v26.X - lh / 1.248, v26.Y - dc / 1.674);
            Vector2 v31 = new Vector2(v30.X - lh / 10.844, v30.Y + dc / 11.942);
            Vector2 v32 = new Vector2(v31.X + lh / 9.823, v31.Y + dc / 3.303);
            Vector2 v33 = new Vector2(v32.X + lh / 3.479, v32.Y + dc / 4.343);
            Vector2 v34 = new Vector2(v32.X + lh / 3.313, v32.Y + dc / 5.132);
            Vector2 v35 = new Vector2(v32.X + lh / 3.436, v32.Y + dc / 3.61);
            Vector2 v36 = new Vector2(v6.X - lh / 4.44, v6.Y);
            Vector2 v37 = new Vector2(v36.X - lh / 1.69, v36.Y - dc / 2.577);
            Vector2 v38 = new Vector2(v36.X - lh / 1.543, v36.Y - dc / 2.654);
            Vector2 v39 = new Vector2(v36.X - lh / 1.48, v36.Y - dc / 5.645);
            Vector2 v41 = new Vector2(v36.X - lh / 2.392, v36.Y);
            Vector2 v40 = new Vector2(v41.X - lh / 5.251, v41.Y - dc / 13.213);
            Vector2 v52 = new Vector2(v8.X - lpr, v8.Y);
            Vector2 v53 = new Vector2(v8.X - lh / 4.97, v6.Y);

            Vector2 v42 = new Vector2(v1.X + oal / 3.055, v1.Y - oal / 1.074);
            Vector2 v43 = new Vector2(v42.X + lh / 0.805, v42.Y);
            Vector2 v44 = new Vector2(v43.X + lh / 8.883, v43.Y - dc / 7.7625);
            Vector2 v47 = new Vector2(v42.X, v42.Y - dc / 0.944);
            Vector2 v46 = new Vector2(v43.X, v47.Y);
            Vector2 v45 = new Vector2(v46.X + lh / 8.883, v46.Y + dc / 7.7625);
            Vector2 v48 = new Vector2(v42.X - lh / 1.596, v42.Y + lh / 3.287);
            Vector2 v49 = new Vector2(v48.X + lh * 3.517, v48.Y);
            Vector2 v50 = new Vector2(v49.X, v49.Y - lh / 0.67);
            Vector2 v51 = new Vector2(v48.X, v50.Y);



            //作图
            //1
            Line line1 = new Line(v1, v2);
            line1.Layer = centre;
            line1.Linetype = Linetype.DashDot;
            line1.LinetypeScale = 3;
            dxf.AddEntity(line1);

            LwPolyline line2 = new LwPolyline();
            line2.Vertexes.Add(new LwPolylineVertex(v5));
            line2.Vertexes.Add(new LwPolylineVertex(v11));
            line2.Vertexes.Add(new LwPolylineVertex(v12));
            line2.Vertexes.Add(new LwPolylineVertex(v4));
            line2.Vertexes.Add(new LwPolylineVertex(v5));
            line2.Vertexes.Add(new LwPolylineVertex(v6));
            line2.Vertexes.Add(new LwPolylineVertex(v7));
            line2.Vertexes.Add(new LwPolylineVertex(v8));
            line2.Vertexes.Add(new LwPolylineVertex(v9));
            line2.Vertexes.Add(new LwPolylineVertex(v10));
            line2.Vertexes.Add(new LwPolylineVertex(v11));
            line2.Vertexes[5].Bulge = Math.Tan(-65 * pi / 180 / 4);
            line2.Vertexes[8].Bulge = Math.Tan(-65 * pi / 180 / 4);
            fun.PolyFill(dxf, line2, 0.1, contour);

            LwPolyline line3 = new LwPolyline();
            line3.Vertexes.Add(new LwPolylineVertex(v6));
            line3.Vertexes.Add(new LwPolylineVertex(v23));
            line3.Vertexes.Add(new LwPolylineVertex(v8));
            line3.Vertexes.Add(new LwPolylineVertex(v13));
            line3.Vertexes.Add(new LwPolylineVertex(v14));
            line3.Vertexes.Add(new LwPolylineVertex(v15));
            line3.Vertexes.Add(new LwPolylineVertex(v16));
            line3.Vertexes[4].Bulge = Math.Tan(-51 * pi / 180 / 4);
            line3.Vertexes[5].Bulge = Math.Tan(-15 * pi / 180 / 4);
            fun.PolyFill(dxf, line3, 0.1, contour);

            Arc c1 = fun.DrawArc3(v16, v17, v13);
            fun.drawBArc(dxf, c1, 0.1, contour);

            Arc c2 = fun.DrawArc3(v8, v20, v19);
            fun.drawBArc(dxf, c2, 0.1, contour);

            LwPolyline line4 = new LwPolyline();
            line4.Vertexes.Add(new LwPolylineVertex(v10));
            line4.Vertexes.Add(new LwPolylineVertex(v15));
            line4.Vertexes.Add(new LwPolylineVertex(v17));
            line4.Vertexes.Add(new LwPolylineVertex(v18));
            line4.Vertexes.Add(new LwPolylineVertex(v19));
            line4.Vertexes[2].Bulge = Math.Tan(89 * pi / 180 / 4);
            line4.Vertexes[3].Bulge = Math.Tan(-79 * pi / 180 / 4);
            fun.PolyFill(dxf, line4, 0.1, contour);

            LwPolyline line5 = new LwPolyline();
            line5.Vertexes.Add(new LwPolylineVertex(v20));
            line5.Vertexes.Add(new LwPolylineVertex(v21));
            line5.Vertexes.Add(new LwPolylineVertex(v22));
            line5.Vertexes.Add(new LwPolylineVertex(v23));
            fun.PolyFill(dxf, line5, 0.1, contour);

            LwPolyline line6 = new LwPolyline();
            line6.Vertexes.Add(new LwPolylineVertex(v21));
            line6.Vertexes.Add(new LwPolylineVertex(v24));
            line6.Vertexes.Add(new LwPolylineVertex(v25));
            line6.Vertexes.Add(new LwPolylineVertex(v26));
            line6.Vertexes.Add(new LwPolylineVertex(v7));
            line6.Vertexes[1].Bulge = Math.Tan(-6 * pi / 180 / 4);
            line6.Vertexes[2].Bulge = Math.Tan(-8 * pi / 180 / 4);
            line6.Vertexes[3].Bulge = Math.Tan(-5 * pi / 180 / 4);
            line6.Vertexes[4].Bulge = Math.Tan(-33 * pi / 180 / 4);
            fun.PolyFill(dxf, line6, 0.1, contour);

            LwPolyline line7 = new LwPolyline();
            line7.Vertexes.Add(new LwPolylineVertex(v28));
            line7.Vertexes.Add(new LwPolylineVertex(v29));
            line7.Vertexes.Add(new LwPolylineVertex(v25));
            line7.Vertexes[1].Bulge = Math.Tan(7 * pi / 180 / 4);
            fun.PolyFill(dxf, line7, 0.1, contour);

            Arc c3 = fun.DrawArc3(v27, v28, v24);
            fun.drawBArc(dxf, c3, 0.1, contour);

            LwPolyline line8 = new LwPolyline();
            line8.Vertexes.Add(new LwPolylineVertex(v26));
            line8.Vertexes.Add(new LwPolylineVertex(v30));
            line8.Vertexes.Add(new LwPolylineVertex(v31));
            line8.Vertexes.Add(new LwPolylineVertex(v32));
            line8.Vertexes.Add(new LwPolylineVertex(v33));
            line8.Vertexes[0].Bulge = Math.Tan(-8 * pi / 180 / 4);
            line8.Vertexes[1].Bulge = Math.Tan(-104 * pi / 180 / 4);
            line8.Vertexes[2].Bulge = Math.Tan(-72 * pi / 180 / 4);
            line8.Vertexes[3].Bulge = Math.Tan(-10 * pi / 180 / 4);
            fun.PolyFill(dxf, line8, 0.1, contour);

            LwPolyline line9 = new LwPolyline();
            line9.Vertexes.Add(new LwPolylineVertex(v7));
            line9.Vertexes.Add(new LwPolylineVertex(v34));
            line9.Vertexes[0].Bulge = Math.Tan(30 * pi / 180 / 4);
            fun.PolyFill(dxf, line9, 0.1, contour);

            Arc c4 = fun.DrawArc3(v35, v33, v34);
            fun.drawBArc(dxf, c4, 0.1, contour);

            LwPolyline line10 = new LwPolyline();
            line10.Vertexes.Add(new LwPolylineVertex(v35));
            line10.Vertexes.Add(new LwPolylineVertex(v6));
            line10.Vertexes[0].Bulge = Math.Tan(-14 * pi / 180 / 4);
            fun.PolyFill(dxf, line10, 0.1, contour);

            LwPolyline line11 = new LwPolyline();
            line11.Vertexes.Add(new LwPolylineVertex(v36));
            line11.Vertexes.Add(new LwPolylineVertex(v37));
            line11.Vertexes.Add(new LwPolylineVertex(v38));
            line11.Vertexes.Add(new LwPolylineVertex(v39));
            line11.Vertexes.Add(new LwPolylineVertex(v40));
            line11.Vertexes.Add(new LwPolylineVertex(v41));
            line11.Vertexes[0].Bulge = Math.Tan(16 * pi / 180 / 4);
            line11.Vertexes[1].Bulge = Math.Tan(-75 * pi / 180 / 4);
            line11.Vertexes[2].Bulge = Math.Tan(-51 * pi / 180 / 4);
            line11.Vertexes[3].Bulge = Math.Tan(-17 * pi / 180 / 4);
            fun.PolyFill(dxf, line11, 0.1, contour);

            LwPolyline line12 = new LwPolyline();
            line12.Vertexes.Add(new LwPolylineVertex(v22));
            line12.Vertexes.Add(new LwPolylineVertex(v26));
            line12.Vertexes.Add(new LwPolylineVertex(v23));
            line12.Vertexes[0].Bulge = Math.Tan(-49 * pi / 180 / 4);
            line12.Vertexes[1].Bulge = Math.Tan(-42 * pi / 180 / 4);
            fun.PolyFill(dxf, line12, 0.1, contour);

            LwPolyline line18 = new LwPolyline();
            line18.Vertexes.Add(new LwPolylineVertex(v37));
            line18.Vertexes.Add(new LwPolylineVertex(v53));
            line18.Vertexes[0].Bulge = Math.Tan(-12 * pi / 180 / 4);
            fun.PolyFill(dxf, line18, 0.1, contour);

            //2
            LwPolyline line13 = new LwPolyline();
            line13.Vertexes.Add(new LwPolylineVertex(v43));
            line13.Vertexes.Add(new LwPolylineVertex(v44));
            line13.Vertexes.Add(new LwPolylineVertex(v45));
            line13.Vertexes.Add(new LwPolylineVertex(v46));
            line13.Vertexes.Add(new LwPolylineVertex(v47));
            line13.Vertexes.Add(new LwPolylineVertex(v42));
            line13.Vertexes.Add(new LwPolylineVertex(v43));
            line13.Vertexes[0].Bulge = Math.Tan(-68 * pi / 180 / 4);
            line13.Vertexes[1].Bulge = Math.Tan(-37 * pi / 180 / 4);
            line13.Vertexes[2].Bulge = Math.Tan(-68 * pi / 180 / 4);
            fun.PolyFill(dxf, line13, 0.1, contour);

            Line line14 = new Line(v48, v49);
            line14.Layer = refin;
            line14.Linetype = Linetype.Dashed;
            line14.LinetypeScale = 5;
            dxf.AddEntity(line14);

            Line line15 = new Line(v49, v50);
            line15.Layer = refin;
            line15.Linetype = Linetype.Dashed;
            line15.LinetypeScale = 5;
            dxf.AddEntity(line15);

            Line line16 = new Line(v48, v51);
            line16.Layer = refin;
            line16.Linetype = Linetype.Dashed;
            line16.LinetypeScale = 5;
            dxf.AddEntity(line16);

            Line line17 = new Line(v50, v51);
            line17.Layer = refin;
            line17.Linetype = Linetype.Dashed;
            line17.LinetypeScale = 5;
            dxf.AddEntity(line17);



            //填充
            LwPolyline hatchpoly1 = new LwPolyline();
            hatchpoly1.Vertexes.Add(new LwPolylineVertex(v43));
            hatchpoly1.Vertexes.Add(new LwPolylineVertex(v44));
            hatchpoly1.Vertexes.Add(new LwPolylineVertex(v45));
            hatchpoly1.Vertexes.Add(new LwPolylineVertex(v46));
            hatchpoly1.Vertexes.Add(new LwPolylineVertex(v47));
            hatchpoly1.Vertexes.Add(new LwPolylineVertex(v42));
            hatchpoly1.Vertexes.Add(new LwPolylineVertex(v43));
            hatchpoly1.Vertexes[0].Bulge = Math.Tan(-68 * pi / 180 / 4);
            hatchpoly1.Vertexes[1].Bulge = Math.Tan(-37 * pi / 180 / 4);
            hatchpoly1.Vertexes[2].Bulge = Math.Tan(-68 * pi / 180 / 4);
            hatchpoly1.IsClosed = true;
            String[] htype1 = new String[100];
            Line[] hline1 = new Line[100];
            Arc[] harc1 = new Arc[100];
            int hnumst1 = hatchpoly1.Explode().Count;  //记录爆炸后，线段有多少
            for (int i = 0; i < hnumst1; i++)
            {
                htype1[i] = hatchpoly1.Explode()[i].Type.ToString();    //将所有的元素记录成一个数组
                if (htype1[i] == "Line")
                {
                    hline1[i] = (Line)hatchpoly1.Explode()[i];
                }
                if (htype1[i] == "Arc")
                {
                    harc1[i] = (Arc)hatchpoly1.Explode()[i];
                }
            }
            fun.hatchsub1(dxf, htype1, hnumst1, 45, 10, hline1, harc1, hatching);



            //坐标系
            fun.Axis(dxf, v3, 90, 180, "", "", "", 0.3, 1, 0);

            fun.Axis(dxf, v52, 90, 180, "", "", "", 0.3, 1, 0);

            fun.Axis(dxf, v8, 90, 180, "", "", "", 0.3, 1, 0);



            //标注

            double height = 2;
            double arrowsize = 2;
            double exline = 2;

            //1
            fun.dimrot1(dxf, v8, v4, 0, oal / 2, 1, height, arrowsize, exline, "OAL,LF", "", "");

            fun.dimrot1(dxf, v52, v8, 0, oal / 5 * 2, 1, height, arrowsize, exline, "LPR,LTA", "", "");

            fun.dimrot1(dxf, v8, v52, 0, oal / 4, 1, height, arrowsize, exline, "APMX", "", "");

            fun.dimrot1(dxf, v5, v11, 90, dconms, 1, height, arrowsize, exline, "DCONMS", "", "");

            fun.dimrot1(dxf, v6, v10, -90, dconms, 1, height, arrowsize, exline, "DC", "", "");

            fun.dimrot1(dxf, v27, v8, 180, oal / 3, 1, height, arrowsize, exline, "LH", "", "");


            //定义格式，路径导出
            dxf.DrawingVariables.AcadVer = DxfVersion.AutoCad2007;
            dxf.Save(path);


        }

    }
}